home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / docs / tidy / examples / .tmpdumpit.php < prev    next >
PHP Script  |  2004-03-24  |  3KB  |  93 lines

  1. <?php
  2.     /*
  3.      * dumpit.php
  4.      *
  5.      * a command-line script which dumps the given HTML, PHP, ASP, XHTML, etc.
  6.      * file as it is represented in the document model.
  7.      *
  8.      * By: John Coggeshall <john@php.net>
  9.      *
  10.      * Usage; php dumpit.php <filename>
  11.      */
  12.      
  13.     tidy_parse_file($_SERVER['argv'][1]);
  14.     
  15.     /* Optionally you can do this here if you want to fix up the document */
  16.     
  17.     /* tidy_clean_repair(); */
  18.               
  19.     $tree = tidy_get_root();
  20.     dump_tree($tree);
  21.     echo "\n";
  22.     
  23.     function node_type($type) {
  24.         
  25.         switch($type) {
  26.             
  27.             case TIDY_NODETYPE_ROOT: return "Root Node";
  28.             case TIDY_NODETYPE_DOCTYPE: return "DocType Node";
  29.             case TIDY_NODETYPE_COMMENT: return "Comment Node";
  30.             case TIDY_NODETYPE_PROCINS: return "ProcIns Node";
  31.             case TIDY_NODETYPE_TEXT: return "Text Node";
  32.             case TIDY_NODETYPE_START: return "Start Node";
  33.             case TIDY_NODETYPE_END: return "End Node";
  34.             case TIDY_NODETYPE_STARTEND: return "Start/End Node";
  35.             case TIDY_NODETYPE_CDATA: return "CDATA Node";
  36.             case TIDY_NODETYPE_SECTION: return "Section Node";
  37.             case TIDY_NODETYPE_ASP: return "ASP Source Code Node";
  38.             case TIDY_NODETYPE_PHP: return "PHP Source Code Node";
  39.             case TIDY_NODETYPE_JSTE: return "JSTE Source Code";
  40.             case TIDY_NODETYPE_XMLDECL: return "XML Declaration Node";
  41.             default: return "Unknown Node";
  42.         }
  43.     }
  44.     
  45.     function do_leaf($string, $indent) {
  46.         for($i = 0; $i < $indent; $i++) {
  47.          echo " ";
  48.         }
  49.         echo $string;
  50.     }
  51.     
  52.     function dump_tree($node, $indent = 0) {
  53.         if($node) {
  54.             /* Put something there if the node name is empty */
  55.             $nodename = trim(strtoupper($node->name));
  56.             $nodename = (empty($nodename)) ? "[EMPTY]" : $nodename;
  57.             
  58.             /* Generate the Node, and a pretty name for it */
  59.             do_leaf(" + $nodename (".node_type($node->type).")\n", $indent);
  60.             
  61.             /* Check to see if this node is a text node. Text nodes are
  62.                generated by start/end tags and contain the text in between.
  63.                i.e. <B>foo</B> will create a text node with $node->value
  64.                equal to 'foo' */
  65.             if($node->type == TIDY_NODETYPE_TEXT) {
  66.                 do_leaf("     |\n", $indent);
  67.                 do_leaf("     +---- Value: '{$node->value}'\n", $indent);
  68.             }
  69.             
  70.             /* Any attributes on this node? */
  71.             if(count($node->attributes())) {
  72.                 do_leaf(" |\n", $indent);
  73.                 do_leaf(" +---- Attributes\n", $indent);
  74.                 
  75.                 /* Cycle through the attributes and display them and their values. */
  76.                 foreach($node->attributes() as $attrib) {
  77.                     do_leaf("             +--{$attrib->name}\n", $indent);
  78.                     do_leaf("             |    +-- Value: {$attrib->value}\n", $indent);
  79.                 }
  80.             }
  81.             
  82.             /* Recurse along the children to generate the remaining nodes */
  83.             if($node->has_children()) {
  84.                 foreach($node->children() as $child) {
  85.                     dump_tree($child, $indent + 3);
  86.                 }
  87.             }
  88.         }
  89.     }
  90.     
  91.     echo tidy_get_output();
  92.  
  93. ?>